package com.efast.dmom.mes.controller;

import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.efast.dmom.mes.bean.ResultBean;
import com.efast.dmom.mes.bean.WmsOutWarehouseOrder;
import com.efast.dmom.mes.service.WmsOutWarehouseOrderService;

@Controller
@RequestMapping("/wmsOutWarehouseOrder")
public class WmsOutWarehouseOrderController {
	
	@Autowired
	private WmsOutWarehouseOrderService wmsOutWarehouseOrderService;
	
	@RequestMapping("/editOrAdd")
	public @ResponseBody ResultBean editOrAdd (@RequestBody List<WmsOutWarehouseOrder> wmsOutWarehouseOrder) {
		ResultBean result = new ResultBean(true);
		try{
			if (wmsOutWarehouseOrder.get(0).getFlag().equals("add")) {
				result = wmsOutWarehouseOrderService.batchSave(wmsOutWarehouseOrder);
			} else if (wmsOutWarehouseOrder.get(0).getFlag().equals("edit")) {
				result = wmsOutWarehouseOrderService.update(wmsOutWarehouseOrder.get(0));
			}
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(e.getMessage());
			return result;
		}
		return result;
	}
	@RequestMapping("/getOrderOutType")
	public @ResponseBody ResultBean getOrderOutType (@RequestBody WmsOutWarehouseOrder wmsOutWarehouseOrder) {
		ResultBean result = new ResultBean(true);
		try{
			result = wmsOutWarehouseOrderService.getOrderOutType(wmsOutWarehouseOrder);
		}catch(Exception e){
			e.printStackTrace();
			result.setSuccess(false);
			result.setMessage(e.getMessage());
		}
		return result;
	}
	
	@RequestMapping("/confirmOrBatchDelete")
	public @ResponseBody ResultBean confirmOrBatchDelete (@RequestBody List<WmsOutWarehouseOrder> wmsOutWarehouseOrder) {
		if (wmsOutWarehouseOrder.get(0).getFlag().equals("batchConfirm")) {
			ResultBean batchConfirm = new ResultBean(true);
			try {
				batchConfirm = wmsOutWarehouseOrderService.batchConfirm(wmsOutWarehouseOrder);
			} catch (Exception e) {
				String message = e.getMessage();
				String substringBefore = StringUtils.substringBefore(message, "!");
				batchConfirm.setErrorMessage(substringBefore);
				return batchConfirm;
			}
				return batchConfirm;
		} else if (wmsOutWarehouseOrder.get(0).getFlag().equals("batchDelete")) {
			ResultBean batchDelete = new ResultBean();
			try {
				batchDelete = wmsOutWarehouseOrderService.batchDelete(wmsOutWarehouseOrder);
			} catch (Exception e) {
				String message = e.getMessage();
				String substringBefore = StringUtils.substringBefore(message, "!");
				batchDelete.setErrorMessage(substringBefore);
				return batchDelete;
			}
			
			return batchDelete;
		}
		return null;
	}
	@RequestMapping("/batchDeleteDetail")
	public @ResponseBody ResultBean confirmOrBatchDeleteDetail (@RequestBody List<WmsOutWarehouseOrder> wmsOutWarehouseOrder) {
		return wmsOutWarehouseOrderService.batchDeleteDetail(wmsOutWarehouseOrder);
	}

}
